package com.stu.vmware;

import com.stu.vmware.bean.*;
import com.stu.vmware.bean.response.TokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.vmware
 * @className: com.stu.vmware-> OpenstackTest
 * @createDate: 2021-12-21 16:55
 * @author: ZhangSheng
 * @version: 1.0
 * @description:
 */
@Slf4j
public class OpenstackTest {

    private RestTemplate restTemplate;


    @Before
    public void before(){
        RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
        restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(5000));
        restTemplateBuilder.setReadTimeout(Duration.ofSeconds(10000));
        restTemplate = restTemplateBuilder.build();
    }

    @Test
    public void create_session(){
        log.info("开始连接openstack创建session");
        TokenAuth tokenAuth = new TokenAuth();
        Identity identity = new Identity();
        Auth auth = new Auth();
        Password password = new Password();
        Domain domain = new Domain();
        User user = new User();
        String [] methods = {"password"};
        domain.setName("Default");
        user.setDomain(domain);
        user.setName("admin");
        user.setPassword("Admin123");
        password.setUser(user);
        identity.setMethods(methods);
        identity.setPassword(password);
        auth.setIdentity(identity);
        tokenAuth.setAuth(auth);
        ResponseEntity<TokenResponse> tokenAuthResponseEntity = restTemplate.postForEntity("http://192.168.156.11:5000/v3/auth/tokens", tokenAuth, TokenResponse.class);
        // XmM7o6X9RTSKu50qfE5b5Q
        if (tokenAuthResponseEntity.getStatusCode().value() != 201){
            log.error("权限校验失败：{} : {} ",tokenAuthResponseEntity.getStatusCode(), tokenAuthResponseEntity.getStatusCode().getReasonPhrase());
        }else if (tokenAuthResponseEntity.getStatusCode().value() == 201){
            log.info("body: {}", tokenAuthResponseEntity.getBody().getToken().getAudit_ids().get(0));
        }
    }


}
